﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class Main_ServiceInvoice_TransactionFormat_InvoiceFormat : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "addnew")
                {

                    PnlAddNew.Visible = true;
                    _BindTdsHeadAccounts();
                    _BindWriteOffAccounts();
                    _BindDdlAdditionTax();

                }
                else if (Request.QueryString["action"].ToString() == "view")
                {
                    PnlView.Visible = true;

                    _BindGridViewData();

                    if (Request.QueryString["msg"] != null)
                    {
                        if (Request.QueryString["msg"].ToString() == "true")
                        {
                            lblMsg.Attributes.Add("class", "true");
                            lblMsg.Text = "Invoice Format Created Successfully..";
                            lblMsg.Focus();
                        }
                        else if (Request.QueryString["msg"].ToString() == "Update")
                        {
                            lblMsg.Attributes.Add("class", "true");
                            lblMsg.Text = "Invoice Format Updated Successfully..";
                            lblMsg.Focus();
                        }
                    }
                    else
                    {
                        lblMsg.Text = "";
                    }

                }
                else if (Request.QueryString["action"].ToString() == "Edit")
                {

                    if (Request.QueryString["FormatId"] != null)
                    {
                        if (Request.QueryString["Status"].ToString() == "Edit")
                        {
                            PnlAddNew.Visible = true;

                            _BindTdsHeadAccounts();
                            _BindWriteOffAccounts();
                            _BindDdlAdditionTax();
                            Page.RegisterStartupScript("myScript", "<script language=JavaScript>EnablePostBillingTab();</script>");
                            _BindEditTranFormat_Main(Request.QueryString["FormatId"].ToString());
                            hidFormatId.Value = Request.QueryString["FormatId"].ToString();
                            btnSave.Text = "Update";

                        }
                    }
                }
            }
        }
    }
    private void _BindTdsHeadAccounts()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TYPE","0")

                                };
        DataTable DT = DU.getdatausingprocedure("USP_IVC_GetTDSAccountLedgerForSettings", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlRoundOff.DataSource = DT;
            ddlRoundOff.DataTextField = "LNAME";
            ddlRoundOff.DataValueField = "LID";
            ddlRoundOff.DataBind();
            ddlRoundOff.Items.Insert(0, new ListItem("", "0"));
        }
    }
    private void _BindWriteOffAccounts()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TYPE","1")

                                };
        DataTable DT = DU.getdatausingprocedure("USP_IVC_GetTDSAccountLedgerForSettings", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlDiscount.DataSource = DT;
            ddlDiscount.DataTextField = "LNAME";
            ddlDiscount.DataValueField = "LID";
            ddlDiscount.DataBind();
            ddlDiscount.Items.Insert(0, new ListItem("", "0"));
        }
    }
    private void _BindDdlAdditionTax()
    {
        DataTable DT = DU.getdatausingprocedure("USP_IVC_GetAdditionalTax");
        ddlAdditionalTax.DataSource = DT;
        ddlAdditionalTax.DataTextField = "MAINCLASSNAME";
        ddlAdditionalTax.DataValueField = "Mainclassid";
        ddlAdditionalTax.DataBind();
        ddlAdditionalTax.Items.Insert(0, new ListItem("", "0"));

    }
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _SaveTranFormatMain(hidFormatId.Value);
                scope.Complete();
            }
            if (btnSave.Text == "Save")
            {
                Response.Redirect("InvoiceFormat.aspx?action=view&msg=true");
            }

            if (btnSave.Text == "Update")
            {
                Response.Redirect("InvoiceFormat.aspx?action=view&msg=Update");
            }

        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }

    }
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
        if (btnSave.Text == "Update")
        {
            Response.Redirect("InvoiceFormat.aspx?action=view");
        }
        else
        {
            Response.Redirect("TransactionFormat.aspx");
        }
    }
    private void _SaveTranFormatMain(string FormatId)
    {
        try
        {
            if (FormatId.ToString() != string.Empty)
            {
                if (_CheckDuplicateFormatName(txtFormatName.Text, FormatId))
                {
                    SqlParameter[] sparam ={                                       
                                   new SqlParameter("@FORMATID",FormatId.ToString()),
                                   new SqlParameter("@FORMATNAME",txtFormatName.Text.Trim()),
                                   new SqlParameter("@SETASDEFAULT",chkDefualt.Checked),
                                   new SqlParameter("@TRANSACTIONTYPE","1"),
                                   new SqlParameter("@YEARREFERENCE",chkYearReference.Checked),
                                   new SqlParameter("@UNITRATE",chkUnitRate.Checked),
                                   new SqlParameter("@ADDDESC",chkAddDescription.Checked),
                                   new SqlParameter("@POSTBILLING",chkPostBillingTDS.Checked),
                                   new SqlParameter("@TAXINCLUSIVE",chkAmtInclusiveTax.Checked),
                                   new SqlParameter("@NONTAXABLE",chkShowNonTaxableAmt.Checked),
                                   new SqlParameter("@UOM",chkUOM.Checked),
                                   new SqlParameter("@NARRATION",chkNarration.Checked),
                                   new SqlParameter("@REMARKS",chkRemarks.Checked),
                                   new SqlParameter("@PREPAREDBY",chkPreparedBy.Checked),
                                   new SqlParameter("@SERVICEDBY",chkServicedBy.Checked),
                                   new SqlParameter("@CHECKEDBY",chkCheckedBy.Checked),
                                   new SqlParameter("@REFERENCENUMBER",chkReferenceNumber.Checked),
                                   new SqlParameter("@PRINTONSAVE",chkPrinOnSave.Checked),
                                   new SqlParameter("@SHOWSERVICECHARGE",chkShowServiceCharges.Checked),
                                   new SqlParameter("@CNAME",Session["username"].ToString())
                               };
                    int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat", sparam);

                    if (FormatId == "0" && Result > 0)
                    {
                        strsql = "Select Ident_current('IVC_TRANSACTIONFORMAT') as FormatId";
                        DataTable DT = DU.getdata(strsql);
                        if (DT.Rows.Count > 0)
                        {
                            FormatId = DT.Rows[0]["FormatId"].ToString();
                        }
                    }
                    if (FormatId != "0")
                    {
                        _SaveTranFormat_AdditionalUnits(FormatId);
                        _SaveTranFormat_Captions(FormatId);
                        _SaveTranFormat_HeadOfAccounts(FormatId);
                        _SaveTranFormat_Numbers(FormatId);
                        _SaveTranFormat_PostBilling(FormatId);
                        _SaveTranFormat_ReportSettings(FormatId);
                        _SaveTranFormat_TaxDiscountDetails(FormatId);
                        _SaveAcc_TransactionMain(FormatId);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void _SaveTranFormat_AdditionalUnits(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@UNIT1",chkAdvancedUnitUnit2.Checked),
                                   new SqlParameter("@UNIT2",chkAdvancedUnitUnit3.Checked),
                                   new SqlParameter("@UNIT3",chkAdvancedUnitUnit4.Checked),
                                   new SqlParameter("@UNIT1MAIN_CAPTION",txtAdvancedUnitAdditionalCap.Text),
                                   new SqlParameter("@UNIT1_FORMULA",txtAdvancedUnitCalculationUnit.Text),
                                   new SqlParameter("@GRSFORMULA",txtAdvancedUnitGrossAmt.Text),
                                   new SqlParameter("@UNIT1_CAPTION",txtAdvancedUnitUnit2.Text),
                                   new SqlParameter("@UNIT2_CAPTION",txtAdvancedUnitUnit3.Text),
                                   new SqlParameter("@UNIT3_CAPTION",txtAdvancedUnitUnit4.Text)                                   
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_AdditionalUnits", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveTranFormat_Captions(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@UNIT",txtCaptionUnit.Text),
                                   new SqlParameter("@RATE",txtCaptionRate.Text),
                                   new SqlParameter("@YEARREFERENCE",txtCaptionYearRef.Text),
                                   new SqlParameter("@ADDDESCRIPTION",txtCaptionAddDesc.Text),
                                   new SqlParameter("@UOM",txtCaptionUOM.Text)
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_Captions", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void _SaveTranFormat_HeadOfAccounts(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@ROUNDOFF_LEDGERID",ddlRoundOff.SelectedValue),
                                   new SqlParameter("@DISCOUNT_LEDGERID",ddlDiscount.SelectedValue),
                                   new SqlParameter("@WRITEOFF_LEDGERID","0"),
                                   new SqlParameter("@TDS_LEDGERID","0")
                                   
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_HeadOfAccounts", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void _SaveTranFormat_Numbers(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString()),
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@NUMBERINGMETHOD",ddlType.SelectedValue),
                                   new SqlParameter("@PREFIX",txtPrefix.Text.Trim()),
                                   new SqlParameter("@STARTNUMBER",txtStartNo.Text.Trim()),
                                   new SqlParameter("@SUFFIX",txtSuffix.Text.Trim()),
                                   new SqlParameter("@NUMWIDTH",txtWidthofNo.Text.Trim()),
                                   new SqlParameter("@PREFILLZERO","0"),
                                   new SqlParameter("@RESETYEAR",chkResetYear.Checked)                                  
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_Numbers", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveTranFormat_PostBilling(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                if (txtPostBillTDS.Text == string.Empty)
                {
                    txtPostBillTDS.Text = "0";
                }
                if (txtPostBillCess.Text == string.Empty)
                {
                    txtPostBillCess.Text = "0";
                }
                if (txtPostBillSurcharge.Text == string.Empty)
                {
                    txtPostBillSurcharge.Text = "0";
                }
                if (txtPostBillEffectiveRate.Text == string.Empty)
                {
                    txtPostBillEffectiveRate.Text = "0";
                }
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@AMTCESS",rbtPostBillDeductTDS.SelectedValue),
                                   new SqlParameter("@TDSRATE",txtPostBillTDS.Text.Trim()),
                                   new SqlParameter("@CESS1RATE",txtPostBillCess.Text.Trim()),
                                   new SqlParameter("@CESS2RATE","0"),
                                   new SqlParameter("@SURCHARGERATE",txtPostBillSurcharge.Text.Trim()),
                                   new SqlParameter("@EFFECTIVERATE",txtPostBillEffectiveRate.Text.Trim())                                   
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_PostBilling", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void _SaveTranFormat_ReportSettings(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@CAPTIONYN",chkBillCaption.Checked),
                                   new SqlParameter("@CAPTION",txtBillCaption.Text.Trim()),
                                   new SqlParameter("@FIRMNAME",txtFirmCaption.Text.Trim()),
                                   new SqlParameter("@INFORMATIONYN",chkInfoBeforeDetails.Checked),
                                   new SqlParameter("@INFORMATION",txtInfoBeforeDetails.Text.Trim()),
                                   new SqlParameter("@NOTEYN",chkNote.Checked),
                                   new SqlParameter("@NOTE",txtNote.Text.Trim()),
                                   new SqlParameter("@LOGOYN",chkPrintLogo.Checked),
                                  // new SqlParameter("@LOGOPATH",strLogoPath),
                                   new SqlParameter("@PAGESETYN",chkPageSettings.Checked),
                                   new SqlParameter("@HEADMARGIN",txtHeaderMargin.Text.Trim()),
                                   new SqlParameter("@FOOTMARGIN",txtFooterMargin.Text.Trim()),
                                   new SqlParameter("@NOOFCOPY",txtNoOfCopy.Text.Trim()),
                                   new SqlParameter("@PAPERSIZE",ddlPaperSize.SelectedValue),
                                   new SqlParameter("@FIRMNAMEADDRYN",chkReportFirmNameAddress.Checked),
                                   new SqlParameter("@CNAMEADDRYN","0"),
                                   new SqlParameter("@PANYN",chkReportPAN.Checked),
                                   new SqlParameter("@STREGNOYN",chkReportSTRegNo.Checked),
                                   new SqlParameter("@OUTSUMMARYYN",chkReportOutStandingSummary.Checked),
                                   new SqlParameter("@SERVICEDBYYN",chkReportServicedBy.Checked),
                                   new SqlParameter("@NARRATIONYN","0"),
                                   new SqlParameter("@PREPAREDBYYN",chkReportPreparedBy.Checked),
                                   new SqlParameter("@CHECKEDBYYN",chkReportCheckedBy.Checked),
                                   new SqlParameter("@REFNOYN",chkReportReferenceNo.Checked)
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_ReportSettings", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveTranFormat_TaxDiscountDetails(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATID",FormatId),
                                   new SqlParameter("@TAXLEVEL",rbtApplyTax.SelectedValue),
                                   new SqlParameter("@DISCOUNTLEVEL",rbtApplyDiscount.SelectedValue),
                                   new SqlParameter("@TAXTYPEID",ddlAdditionalTax.SelectedValue),
                                   new SqlParameter("@TAXCALON","0"),
                                   new SqlParameter("@ITEMTAXTYPE",rbtServiceTaxSettings.SelectedValue)                                
                               };
                int Result = DU.executeprocedure("USP_IVC_SaveTransactionFormat_Tax_Discount_Details", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private bool _CheckDuplicateFormatName(string FormatName, string FormatId)
    {
        bool flag = true;
        SqlParameter[] sparam ={
                                   new SqlParameter("@FORMATNAME",FormatName),
                                   new SqlParameter("@FORMATID",FormatId)
                              };
        DataTable DTDuplicate = DU.getdatausingprocedure("USP_IVC_IsFormatNameExists", sparam);
        if (DTDuplicate.Rows.Count > 0 && Convert.ToDouble(DTDuplicate.Rows[0]["FormatName"].ToString()) > 0)
        {
            lblMsg.Text = "Duplicate Format Name";
            flag = false;
        }
        else
        {
            flag = true;
        }
        return flag;
    }
    private void _SaveAcc_TransactionMain(string FormatId)
    {
        try
        {
            if (FormatId != string.Empty)
            {
                string[] Date1 = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(System.DateTime.Now)).ToString().Split('/');
                string effectiveDate = Date1[0].ToString() + "/" + Date1[1].ToString() + "/" + Date1[2].ToString();

                SqlParameter[] sparam =
                                        {
                                            
                                            new SqlParameter("@TYPEID",hidTypeId.Value),
                                            new SqlParameter("@PARENTTYPEID","10"),
                                            new SqlParameter("@TRANSACTIONNAME",txtFormatName.Text.Trim()),
                                            new SqlParameter("@ISAUTONO",ddlType.SelectedValue),
                                            new SqlParameter("@RESETNO",chkResetYear.Checked),
                                            new SqlParameter("@ISREFERENCE","0"),                                                
                                            new SqlParameter("@ISDEFAULTNARRATION","0"),
                                            new SqlParameter("@EFFECTIVEDATE",effectiveDate),
                                            new SqlParameter("@BILLFORMATID",FormatId),
                                            new SqlParameter("@MODULETYPE","3"),
                                            new SqlParameter("@CCALLOCATIONTYPE","0")
                                        };
                string strTypeId = DU.ExecuteProcedure_Scope_Identity("USP_ACC_SaveTransactionSetting", sparam);
                if (strTypeId != string.Empty)
                {
                    _SaveAcc_TransactionDetails(strTypeId);

                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void _SaveAcc_TransactionDetails(string strAccTypeId)
    {
        try
        {
            if (strAccTypeId != string.Empty)
            {
                SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TYPEID",strAccTypeId),
                                            new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString()),
                                            new SqlParameter("@STARTNO",txtStartNo.Text.Trim()),
                                            new SqlParameter("@PREFIX",txtPrefix.Text.Trim()),
                                            new SqlParameter("@SUFFIX",txtSuffix.Text.Trim()),
                                            new SqlParameter("@ACCLEVELNARRATION","0"),
                                            new SqlParameter("@PRINTONSAVE",chkPrinOnSave.Checked)

                                        };
                int result = DU.executeprocedure("USP_ACC_SaveTransactionSettingDetail", sparam);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    //***************Bind Data For Edit***********

    private void _BindEditTranFormat_Main(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@FORMATID",strFormatId)
                                   };
            DataTable DTFormatMain = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat", sparam);
            if (DTFormatMain.Rows.Count > 0)
            {
                txtFormatName.Text = DTFormatMain.Rows[0]["FORMATNAME"].ToString();
                chkDefualt.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["SETASDEFAULT"].ToString());
                chkYearReference.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["YEARREFERENCE"].ToString());
                chkUnitRate.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["UNITRATE"].ToString());
                chkAddDescription.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["ADDDESC"].ToString());
                chkPostBillingTDS.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["POSTBILLING"].ToString());
                chkAmtInclusiveTax.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["TAXINCLUSIVE"].ToString());
                chkShowNonTaxableAmt.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["NONTAXABLE"].ToString());
                chkUOM.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["UOM"].ToString());
                chkServicedBy.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["SERVICEDBY"].ToString());
                chkAmtInclusiveTax.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["TAXINCLUSIVE"].ToString());
                chkShowServiceCharges.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["SHOWSERVICECHARGE"].ToString());
                chkPreparedBy.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["PREPAREDBY"].ToString());
                chkPrinOnSave.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["PRINTONSAVE"].ToString());
                chkReferenceNumber.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["REFERENCENUMBER"].ToString());
                chkRemarks.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["REMARKS"].ToString());
                chkNarration.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["NARRATION"].ToString());
                chkCheckedBy.Checked = Convert.ToBoolean(DTFormatMain.Rows[0]["CHECKEDBY"].ToString());


                _BindEditTranFormat_TaxDiscounts(strFormatId);
                _BindEditTranFormat_Caption(strFormatId);
                _BindEditTranFormat_Numbers(strFormatId);
                _BindEditTranFormat_HeadOfAccounts(strFormatId);
                _BindEditTranFormat_PostBilling(strFormatId);
                _BindEditTranFormat_ReportSettings(strFormatId);
                _BindEditTranFormat_Advanceunit(strFormatId);

                SqlParameter[] sparam1 ={
                                        new SqlParameter("@PARENTTYPEID","10"),
                                        new SqlParameter("@BILLFORMATID",strFormatId),
                                        new SqlParameter("@MODULETYPE","3")
                                   };
                DataTable DTTypeId = DU.getdatausingprocedure("USP_ACC_GetTransactionSettingBasedOnFormat", sparam1);
                if (DTTypeId.Rows.Count > 0)
                {
                    hidTypeId.Value = DTTypeId.Rows[0]["TYPEID"].ToString();
                }
            }
        }
    }

    private void _BindEditTranFormat_TaxDiscounts(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@FORMATID",strFormatId)
                                   };
            DataTable DTFormatMain = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_Tax_Discount_Details", sparam);
            if (DTFormatMain.Rows.Count > 0)
            {
                rbtApplyTax.SelectedValue = DTFormatMain.Rows[0]["TAXLEVEL"].ToString();
                rbtApplyDiscount.SelectedValue = DTFormatMain.Rows[0]["DISCOUNTLEVEL"].ToString();
                ddlAdditionalTax.SelectedValue = DTFormatMain.Rows[0]["TAXTYPEID"].ToString();
                if (DTFormatMain.Rows[0]["TAXLEVEL"].ToString() == "True")
                {
                    rbtApplyTax.Items[1].Selected = true;
                    panelAdditionalTax.Attributes.Add("display", "none");
                    panelServiceTax.Attributes.Add("display", "inline");
                    panelServiceTax.Visible = true;
                    rbtServiceTaxSettings.SelectedValue = DTFormatMain.Rows[0]["ITEMTAXTYPE"].ToString();
                    rbtApplyTax.Items[1].Selected = true;
                    panelServiceTax.Visible = true;
                    panelServiceTax.Style.Add("display", "inline");
                    panelAdditionalTax.Style.Add("display", "none");
                    rbtServiceTaxSettings.SelectedValue = DTFormatMain.Rows[0]["ItemTaxType"].ToString();
                }
            }
        }
    }

    private void _BindEditTranFormat_Numbers(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                        new SqlParameter("@FORMATID",strFormatId),
                                        new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString())
                                   };
            DataTable DTFormatNumber = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_Numbers", sparam);
            if (DTFormatNumber.Rows.Count > 0)
            {
                txtPrefix.Text = DTFormatNumber.Rows[0]["PREFIX"].ToString();
                txtStartNo.Text = DTFormatNumber.Rows[0]["STARTNUMBER"].ToString();
                txtSuffix.Text = DTFormatNumber.Rows[0]["SUFFIX"].ToString();
                txtWidthofNo.Text = DTFormatNumber.Rows[0]["NUMWIDTH"].ToString();
                chkResetYear.Checked = Convert.ToBoolean(DTFormatNumber.Rows[0]["RESETYEAR"].ToString());
                ddlType.SelectedValue = DTFormatNumber.Rows[0]["NUMBERINGMETHOD"].ToString();

            }
        }
    }
    private void _BindEditTranFormat_HeadOfAccounts(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@FORMATID",strFormatId)
                                   };
            DataTable DTFormatHead = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_HeadOfAccounts", sparam);
            if (DTFormatHead.Rows.Count > 0)
            {
                ddlRoundOff.SelectedValue = DTFormatHead.Rows[0]["ROUNDOFF_LEDGERID"].ToString();
                ddlDiscount.SelectedValue = DTFormatHead.Rows[0]["DISCOUNT_LEDGERID"].ToString();

            }
        }
    }

    private void _BindEditTranFormat_Caption(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                        new SqlParameter("@FORMATID",strFormatId)
                                        
                                   };
            DataTable DTFormatReport = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_Captions", sparam);
            if (DTFormatReport.Rows.Count > 0)
            {
                txtCaptionRate.Text = DTFormatReport.Rows[0]["RATE"].ToString();
                txtCaptionUnit.Text = DTFormatReport.Rows[0]["UNIT"].ToString();
                txtCaptionYearRef.Text = DTFormatReport.Rows[0]["YEARREFERENCE"].ToString();
                txtCaptionAddDesc.Text = DTFormatReport.Rows[0]["ADDDESCRIPTION"].ToString();
                txtCaptionUOM.Text = DTFormatReport.Rows[0]["UOM"].ToString();
            }
        }
    }

    private void _BindEditTranFormat_PostBilling(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                        new SqlParameter("@FORMATID",strFormatId)
                                        
                                   };
            DataTable DTFormatReport = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_PostBilling", sparam);
            if (DTFormatReport.Rows.Count > 0)
            {
                txtPostBillTDS.Text = DTFormatReport.Rows[0]["TDSRATE"].ToString();
                txtPostBillCess.Text = DTFormatReport.Rows[0]["CESS1RATE"].ToString();
                rbtPostBillDeductTDS.SelectedValue = DTFormatReport.Rows[0]["AMTCESS"].ToString();
                txtPostBillSurcharge.Text = DTFormatReport.Rows[0]["SURCHARGERATE"].ToString();
                txtPostBillEffectiveRate.Text = DTFormatReport.Rows[0]["EFFECTIVERATE"].ToString();
            }
        }
    }

    private void _BindEditTranFormat_Advanceunit(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                        new SqlParameter("@FORMATID",strFormatId)
                                        
                                   };
            DataTable DTFormatReport = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_AdditionalUnits", sparam);
            if (DTFormatReport.Rows.Count > 0)
            {
                chkAdvancedUnitUnit2.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["UNIT1"].ToString());
                chkAdvancedUnitUnit3.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["UNIT2"].ToString());
                chkAdvancedUnitUnit4.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["UNIT3"].ToString());

                txtAdvancedUnitAdditionalCap.Text = DTFormatReport.Rows[0]["UNIT1MAIN_CAPTION"].ToString();
                txtAdvancedUnitCalculationUnit.Text = DTFormatReport.Rows[0]["UNIT1_FORMULA"].ToString();
                txtAdvancedUnitGrossAmt.Text = DTFormatReport.Rows[0]["GRSFORMULA"].ToString();
                txtAdvancedUnitUnit2.Text = DTFormatReport.Rows[0]["UNIT1_CAPTION"].ToString();
                txtAdvancedUnitUnit3.Text = DTFormatReport.Rows[0]["UNIT2_CAPTION"].ToString();
                txtAdvancedUnitUnit4.Text = DTFormatReport.Rows[0]["UNIT3_CAPTION"].ToString();
            }
        }
    }

    private void _BindEditTranFormat_ReportSettings(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                        new SqlParameter("@FORMATID",strFormatId)
                                        
                                   };
            DataTable DTFormatReport = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_ReportSettings", sparam);
            if (DTFormatReport.Rows.Count > 0)
            {
                chkBillCaption.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["CAPTIONYN"].ToString());
                if (DTFormatReport.Rows[0]["CAPTIONYN"].ToString() == "True")
                {
                    txtBillCaption.Enabled = true;
                    txtBillCaption.Text = DTFormatReport.Rows[0]["CAPTION"].ToString();
                }
                else
                {
                    txtBillCaption.Enabled = false;
                }

                txtFirmCaption.Text = DTFormatReport.Rows[0]["FIRMNAME"].ToString();
                chkInfoBeforeDetails.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["INFORMATIONYN"].ToString());
                if (DTFormatReport.Rows[0]["INFORMATIONYN"].ToString() == "True")
                {
                    txtInfoBeforeDetails.Enabled = true;
                    txtInfoBeforeDetails.Text = DTFormatReport.Rows[0]["INFORMATION"].ToString();
                }
                else
                {
                    txtInfoBeforeDetails.Enabled = false;
                }


                chkNote.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["NOTEYN"].ToString());

                if (DTFormatReport.Rows[0]["NOTEYN"].ToString() == "True")
                {
                    txtNote.Enabled = true;
                    txtNote.Text = DTFormatReport.Rows[0]["NOTE"].ToString();
                }
                else
                {
                    txtNote.Enabled = false;
                }

                chkPrintLogo.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["LOGOYN"].ToString());
                // hidFileUpload.Value = DTFormatReport.Rows[0]["LOGOPATH"].ToString();
                chkPageSettings.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["PAGESETYN"].ToString());
                if (DTFormatReport.Rows[0]["PAGESETYN"].ToString() == "True")
                {
                    txtHeaderMargin.Text = "0.5";
                    txtHeaderMargin.Enabled = false;
                    txtFooterMargin.Text = "0.5";
                    txtFooterMargin.Enabled = false;
                }
                else
                {
                    txtHeaderMargin.Enabled = true;
                    txtHeaderMargin.Text = DTFormatReport.Rows[0]["HEADMARGIN"].ToString();
                    txtFooterMargin.Text = DTFormatReport.Rows[0]["FOOTMARGIN"].ToString();
                    txtFooterMargin.Enabled = true;
                }
                txtNoOfCopy.Text = DTFormatReport.Rows[0]["NOOFCOPY"].ToString();


                if (DTFormatReport.Rows[0]["PAPERSIZE"].ToString() == "True")
                {
                    ddlPaperSize.SelectedValue = "1";
                }


                chkReportCheckedBy.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["CHECKEDBYYN"].ToString());
                chkReportFirmNameAddress.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["FIRMNAMEADDRYN"].ToString());
                chkReportOutStandingSummary.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["OUTSUMMARYYN"].ToString());
                chkReportPreparedBy.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["PREPAREDBYYN"].ToString());
                chkReportSTRegNo.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["STREGNOYN"].ToString());

                chkReportPAN.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["PANYN"].ToString());
                chkReportServicedBy.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["SERVICEDBYYN"].ToString());
                chkReportReferenceNo.Checked = Convert.ToBoolean(DTFormatReport.Rows[0]["REFNOYN"].ToString());
            }
        }
    }

    private void _BindGridViewData()
    {
        SqlParameter[] sparam ={
                                       new SqlParameter("@TRANSACTIONTYPE","1")
                                   };
        DataTable DTFormatMain = DU.getdatausingprocedure("USP_IVC_GetAllTransactionFormats", sparam);
        grdDisplay.DataSource = DTFormatMain;
        grdDisplay.DataBind();
    }
    protected void grdDisplay_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[2].Text == "True")
            {
                e.Row.Cells[2].Text = "Yes";
            }
            else
            {
                e.Row.Cells[2].Text = "No";
            }
            if (e.Row.Cells[3].Text == "True")
            {
                e.Row.Cells[3].Text = "Yes";
            }
            else
            {
                e.Row.Cells[3].Text = "No";
            }
            if (e.Row.Cells[4].Text == "True")
            {
                e.Row.Cells[4].Text = "Yes";
            }
            else
            {
                e.Row.Cells[4].Text = "No";
            }
            if (e.Row.Cells[5].Text == "True")
            {
                e.Row.Cells[5].Text = "Yes";
            }
            else
            {
                e.Row.Cells[5].Text = "No";
            }
            if (e.Row.Cells[6].Text == "True")
            {
                e.Row.Cells[6].Text = "Yes";
            }
            else
            {
                e.Row.Cells[6].Text = "No";
            }
            if (e.Row.Cells[7].Text == "True")
            {
                e.Row.Cells[7].Text = "Yes";
            }
            else
            {
                e.Row.Cells[7].Text = "No";
            }

            if (e.Row.Cells[8].Text == "True")
            {
                e.Row.Cells[8].Text = "Yes";
            }
            else
            {
                e.Row.Cells[8].Text = "No";
            }
        }
    }
    protected void grdDidplay_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string strDataKey = grdDisplay.SelectedDataKey.Value.ToString();
        Response.Redirect("InvoiceFormat.aspx?Status=Edit&action=Edit&FormatId=" + strDataKey);
    }
}
